﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using PepsiCo.Common.Services;
using System.Collections.Generic;
using PepsiCo.Common.Entities;
using Microsoft.SharePoint.Utilities;
using T2V.SharePoint.Common.Utilities;
using PepsiCo.Common;

namespace PepsiCo.Intranet.WebParts.ShowRecentNewsByCategory
{
    public partial class ShowRecentNewsByCategoryUserControl : UserControl
    {
        #region [Properties]
        public ShowRecentNewsByCategory WebPart { get; set; }

        public string FilterNewsCategory
        {
            get
            {
                string cat = string.Empty;

                if (WebPart != null && !string.IsNullOrEmpty(WebPart.FilterNewsCategory))
                {
                    cat = WebPart.FilterNewsCategory;
                }

                return cat;
            }
        }

        public bool ShowHotNewsOnly
        {
            get
            {
                bool show = true;

                if (WebPart != null)
                {
                    show = WebPart.ShowHotNewsOnly;
                }

                return show;
            }
        }

        public int ItemCount
        {
            get
            {
                int number = 10;

                if (WebPart != null)
                {
                    int.TryParse(WebPart.ItemCount, out number);
                }

                return number;
            }
        }

        public bool ShowDateTime
        {
            get
            {
                bool show = true;

                if (WebPart != null)
                {
                    show = WebPart.ShowDateTime;
                }

                return show;
            }
        }

        public string DateTimeFormat
        {
            get
            {
                string d = "MMM dd, yyyy";

                if (WebPart != null && !string.IsNullOrEmpty(WebPart.DateTimeFormat))
                {
                    d = WebPart.DateTimeFormat;
                }

                return d;
            }
        }
        #endregion [Properties]

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            int rowCount = ItemCount;

            SPListItemCollection collection;

            SPList listNews = Utility.GetListFromURL(PepsiCoConstants.NEWS_LIST_URL, SPContext.Current.Web);

            List<NewsItem> catNews = NewsService.GetNewsByCategory(listNews, FilterNewsCategory, ShowHotNewsOnly, rowCount, string.Empty, out collection);

            if (catNews != null && catNews.Count > 0)
            {
                rptNews.DataSource = catNews;
                rptNews.DataBind();
            }
        }
    }
}
